package com.carrey.cms_1810.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.carrey.carrey_utils.util.RandomUtil;
import com.carrey.cms_1810.entity.Article;
import com.carrey.cms_1810.entity.Category;
import com.carrey.cms_1810.entity.Channel;
import com.carrey.cms_1810.entity.Collect;
import com.carrey.cms_1810.entity.Comments;
import com.carrey.cms_1810.entity.User;
import com.carrey.cms_1810.service.ArticleService;
import com.carrey.cms_1810.service.CategoryService;
import com.carrey.cms_1810.service.ChannelService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("my")
public class MyController {

	@Autowired
	private ChannelService channelService;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private ArticleService articleService;

	// 跳转到个人中心首页
	@RequestMapping({ "/", "", "index" })
	public String index() {
		return "my/index";
	}

	// 跳转到发布页面，只有get请求才能访问的
	@GetMapping("publish")
	public String publish() {
		return "my/publish";
	}

	// 获取发布文章需要的频道数据，频道分类
	// 查询所有的栏目数据
	@RequestMapping("getChannels")
	@ResponseBody
	public List<Channel> getChannels() {
		// 查询所有的栏目数据
		List<Channel> list = channelService.getChannels();
		return list;
	}

	// 根据栏目的id获取，该栏目下所有的分类
	@RequestMapping("getCategoryByChannelId")
	@ResponseBody
	public List<Category> getCategoryByChannelId(Integer channelId) {
		return categoryService.getCategorysByChannelId(channelId);
	}

	// 发布文章，接收图片
	@RequestMapping("publish")
	@ResponseBody
	public boolean publish(Article article, MultipartFile file, HttpSession session)
			throws IllegalStateException, IOException {
		// 处理图片
		if (file != null && file.getSize() > 0) {
			// 获取原文件名
			String filename = file.getOriginalFilename();
			// 使用uuid生成随机文件名
			filename = RandomUtil.getUUID() + filename.substring(filename.lastIndexOf("."));
			// 获取目标文件
			File f = new File("d:/picture/" + filename);
			// 复制文件
			file.transferTo(f);
			// 将文件名存入到实体类中
			article.setPicture("/picture/" + filename);
		}
		// 设置文章其他字段
		User user = (User) session.getAttribute("LOGIN_USER");
		article.setUserId(user.getId());// 从session中获取对象
		// 文章属性赋默认值
		article.setHits(0);// 点击量
		article.setHot(0);// 是否热门
		article.setStatus(0);// 0,刚发布,1审核通过,-1 审核未通过
		article.setDeleted(0);// 0:正常,1:删除
		article.setCreated(new Date());// 文章创建日期
		article.setContentType(0);// 文章类型: 0:html 1:json
		article.setComments(0);// 评论数量
		// 将文章存入到数据库
		boolean result = articleService.add(article);
		return result;
	}

	// 查询当前用户的所有文章的列表
	@RequestMapping("articles")
	public String articles(Model m, HttpSession session, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize) {
		// 从session中获取用户
		User user = (User) session.getAttribute("LOGIN_USER");
		// 查询数据
		PageInfo<Article> info = articleService.listByUser(pageNum, pageSize, user);
		// 将数据发送到jsp页面
		m.addAttribute("info", info);
		return "my/articles";
	}

	// 根据文章id查询文章数据
	@RequestMapping("detail")
	@ResponseBody
	public Article detail(Integer id) {
		return articleService.getDetailById(id);
	}

	// 添加收藏
	@RequestMapping("addCollect")
	@ResponseBody
	public Collect addCollect(Collect collect, HttpSession session) {
		// 获取登录用户
		User user = (User) session.getAttribute("LOGIN_USER");
		// 设置用户
		collect.setUserId(user.getId());
		// 设置路径
		collect.setUrl("http://localhost:80/detail?articleId=" + collect.getArticleId());
		// 设置时间
		collect.setCreated(new Date());
		// 添加收藏
		articleService.addCollect(collect);
		return collect;
	}

	// 取消收藏
	@RequestMapping("delCollect")
	@ResponseBody
	public boolean delCollect(Collect collect) {
		// 取消收藏
		return articleService.delCollect(collect);
	}

	// 查看收藏
	@RequestMapping("getCollectByArticleId")
	@ResponseBody
	public Collect getCollectByArticleId(Collect collect, HttpSession session) {
		// 获取登录用户
		User user = (User) session.getAttribute("LOGIN_USER");
		// 设置用户
		collect.setUserId(user.getId());
		return articleService.getCollectByArticleId(collect);
	}

	// 获取用户的所有收藏
	@RequestMapping("collect")
	public String collect(Model m, HttpSession session, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "3") Integer pageSize) {
		// 获取登录后的user对象
		User user = (User) session.getAttribute("LOGIN_USER");
		// 根据用户id查询其发布的所有文章
		PageInfo<Collect> info = articleService.getCollect(pageNum, pageSize, user);
		// 将数据传到jsp页面
		m.addAttribute("info", info);
		return "my/collect";
	}

	// 查询我的评论
	@RequestMapping("comment")
	public String comment(Model m, HttpSession session, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "3") Integer pageSize) {
		// 获取登录后的user对象
		User user = (User) session.getAttribute("LOGIN_USER");
		// 根据用户id查询其发布的所有文章
		PageInfo<Comments> info = articleService.getCommentByUser(pageNum, pageSize, user);
		// 将数据传到jsp页面
		m.addAttribute("info", info);
		return "my/comment";
	}

}
